<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>

<!--
	Copyright (c) 1999-2008 by Digital Mars
	All Rights Reserved
	Written by Walter Bright
	www.digitalmars.com
  -->

<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" >
<title>std.signals - D Programming Language - Digital Mars</title>
<link rel="stylesheet" type="text/css" href="./style.css">
<link rel="icon" type="image/gif" href="./holy.gif" />
</head>

<body>
<div id="heading">
	<a href="http://www.digitalmars.com/"><img src="./dmlogo.gif" width="270" height="53" border="0" alt="www.digitalmars.com" align="left"></a>
	<p align="right">D Programming Language 1.0</p>


	<div id="headingNav">
	<ul>
	<li><a href="http://www.prowiki.org/wiki4d/wiki.cgi?DocComments/Phobos/StdSignals" title="Read/write comments and feedback">Comments</a></li>
	<li><a href="http://www.digitalmars.com/d/1.0/index.html" title="D Programming Language" class="dlink">D</a></li>
	<li><a href="http://www.digitalmars.com/advancedsearch.html" title="Search Digital Mars web site">Search</a></li>
	<li><a href="http://www.digitalmars.com/" title="www.digitalmars.com">Home</a></li>
	</ul>
	</div>

	<div id="lastupdate">Last update Wed Oct  1 23:47:52 2008</div>
</div>

<!-- -->

<div id="navigation">
    <div class="navblock">
<form method="get" action="http://www.google.com/search">
<div id="searchbox">
<input id="q" name="q" size="10" value="RTL Search" onFocus='if(this.value == "RTL Search"){this.value="";}'>
<input type="hidden" id="domains" name="domains" value="www.digitalmars.com">
<input type="hidden" id="sitesearch" name="sitesearch" value="www.digitalmars.com/d/1.0/phobos">
<input type="hidden" id="sourceid" name="sourceid" value="google-search">
<input type="submit" id="submit" name="submit" value="Go">
</div>
</form>
<div id="toctop">
    <ul>
	<li><a href="http://www.digitalmars.com/d/1.0/index.html" title="D Programming Language">D ...</a></li>
	<li><a href="http://www.digitalmars.com/d/1.0/lex.html" title="D Language Specification">Language ...</a></li>
	<li><a href="phobos.html" title="D Runtime Library">Phobos ...</a></li>
	<li><a href="http://www.digitalmars.com/d/1.0/comparison.html" title="Language Comparisons">Comparisons ...</a></li>
    </ul>
</div>
</div>
    <div class="navblock">
<ul>
<li><a href="index.html" title="Index of all HTML files">Index</a></li>
</ul>
    <ul>
	<li><a href="object.html" title="root of object hierarchy">object</a></li>
    </ul>
    <h2><a href="phobos.html#std" title="D standard modules">std</a></h2>
    <ul>
	<li><a href="std.base64.html" title="Encode/decode base64 format">std.base64</a></li>
	<li><a href="std.bind.html" title="Function argument binding">std.bind</a></li>
	<li><a href="std.bitarray.html" title="Arrays of bits">std.bitarray</a></li>
	<li><a href="std.boxer.html" title="Box/unbox types">std.boxer</a></li>
	<li><a href="std.compiler.html" title="Information about the D compiler implementation">std.compiler</a></li>
	<li><a href="std.conv.html" title="Conversion of strings to integers">std.conv</a></li>
	<li><a href="std.cover.html" title="D coverage analyzer">std.cover</a></li>
	<li><a href="std.cpuid.html" title="CPU identification">std.cpuid</a></li>
	<li><a href="std.ctype.html" title="Simple character classification">std.ctype</a></li>
	<li><a href="std.date.html" title="Date and time functions">std.date</a></li>
	<li><a href="std.demangle.html" title="Demangle D names">std.demangle</a></li>
	<li><a href="std.file.html" title="Basic file operations">std.file</a></li>
	<li><a href="std.format.html" title="Formatted conversions of values to strings">std.format</a></li>
	<li><a href="std.gc.html" title="Control the garbage collector">std.gc</a></li>
	<li><a href="std.intrinsic.html" title="Compiler built in intrinsic functions">std.intrinsic</a></li>
	<li><a href="std.math.html" title="the usual math functions">std.math</a></li>
	<li><a href="std.md5.html" title="Compute MD5 digests">std.md5</a></li>
	<li><a href="std.metastrings.html" title="Metaprogramming with strings">std.metastrings</a></li>
	<li><a href="std.mmfile.html" title="Memory mapped files">std.mmfile</a></li>
	<li><a href="std.openrj.html" title="Basic database">std.openrj</a></li>
	<li><a href="std.outbuffer.html" title="Assemble data into an array of bytes">std.outbuffer</a></li>
	<li><a href="std.outofmemory.html" title="Out of memory exception">std.outofmemory</a></li>
	<li><a href="std.path.html" title="Manipulate file names, path names, etc.">std.path</a></li>
	<li><a href="std.process.html" title="Create/destroy threads">std.process</a></li>
	<li><a href="std.random.html" title="Random number generation">std.random</a></li>
	<li><a href="std.regexp.html" title="regular expressions">std.regexp</a></li>
	<li><a href="std.signals.html" title="Signals">std.signals</a></li>
	<li><a href="std.socket.html" title="Sockets">std.socket</a></li>
	<li><a href="std.socketstream.html" title="Stream for a blocking, connected Socket">std.socketstream</a></li>
	<li><a href="std.stdint.html" title="Integral types for various purposes">std.stdint</a></li>
	<li><a href="std.stdio.html" title="Standard I/O">std.stdio</a></li>
	<li><a href="std.cstream.html" title="Stream I/O">std.cstream</a></li>
	<li><a href="std.stream.html" title="Stream I/O">std.stream</a></li>
	<li><a href="std.string.html" title="Basic string operations">std.string</a></li>
	<li><a href="std.system.html" title="Inquire about the CPU, operating system">std.system</a></li>
	<li><a href="std.thread.html" title="Thread operations">std.thread</a></li>
	<li><a href="std.traits.html" title="Type traits">std.traits</a></li>
	<li><a href="std.typetuple.html" title="Type tuples">std.typetuple</a></li>
	<li><a href="std.uni.html" title="Unicode classification">std.uni</a></li>
	<li><a href="std.uri.html" title="Encode and decode Uniform Resource Identifiers (URIs)">std.uri</a></li>
	<li><a href="std.utf.html" title="Encode and decode utf character encodings">std.utf</a></li>
	<li><a href="std.zip.html" title="Read/write zip archives">std.zip</a></li>
	<li><a href="std.zlib.html" title="Compression / Decompression of data">std.zlib</a></li>
	<li><a href="std.c.fenv.html" title="Floating point environment">std.c.fenv</a></li>
	<li><a href="std.c.locale.html" title="Locale">std.c.locale</a></li>
	<li><a href="std.c.math.html" title="Math">std.c.math</a></li>
	<li><a href="std.c.process.html" title="Process">std.c.process</a></li>
	<li><a href="std.c.stdarg.html" title="Variadic arguments">std.c.stdarg</a></li>
	<li><a href="std.c.stddef.html" title="Standard definitions">std.c.stddef</a></li>
	<li><a href="std.c.stdio.html" title="Standard I/O">std.c.stdio</a></li>
	<li><a href="std.c.stdlib.html" title="Standard library">std.c.stdlib</a></li>
	<li><a href="std.c.string.html" title="Strings">std.c.string</a></li>
	<li><a href="std.c.time.html" title="Time">std.c.time</a></li>
	<li><a href="std.c.wcharh.html" title="Wide characters">std.c.wcharh</a></li>
	<li><a href="std.windows.charset.html" title="Conversion to/from Windows character sets">std.windows.charset</a></li>
	<li><a href="phobos.html#std_windows" title="Modules specific to Windows">std.windows</a></li>
	<li><a href="phobos.html#std_linux" title="Modules specific to Windows">std.linux</a></li>
	<li><a href="phobos.html#std_c_windows" title="C Windows API">std.c.windows</a></li>
	<li><a href="phobos.html#std_c_linux" title="C Linux API">std.c.linux</a></li>
    </ul>
</div>
</div>
<div id="content">
    <h1><a href="./htmlsrc/std.signals.html">std.signals</a></h1>
    
Signals and Slots are an implementation of the Observer Pattern.
 Essentially, when a Signal is emitted, a list of connected Observers
 &#40;called slots&#41; are called.<br><br>
There have been several D implementations of Signals and Slots.
 This version makes use of several new features in D, which make
 using it simpler and less error prone. In particular, it is no
 longer necessary to instrument the slots.<br><br> <br><br>
<b>References:</b><br><a href="http://scottcollins.net/articles/a-deeper-look-at-_signals-and-slots.html">A Deeper Look at Signals and Slots</a><br>
	<a href="http://en.wikipedia.org/wiki/Observer_pattern">Observer pattern</a><br>
	<a href="http://en.wikipedia.org/wiki/Signals_and_slots">Wikipedia</a><br>
	<a href="http://boost.org/doc/html/signals.html">Boost Signals</a><br>
	<a href="http://doc.trolltech.com/4.1/signalsandslots.html">Qt</a><br><br><br>	There has been a great deal of discussion in the D newsgroups
	over this, and several implementations:<br><br>	<a href="http://www.digitalmars.com/d/archives/digitalmars/D/announce/signal_slots_library_4825.html">signal slots library</a><br>
	<a href="http://www.digitalmars.com/d/archives/digitalmars/D/Signals_and_Slots_in_D_42387.html">Signals and Slots in D</a><br>
	<a href="http://www.digitalmars.com/d/archives/digitalmars/D/Dynamic_binding_--_Qt_s_Signals_and_Slots_vs_Objective-C_42260.html">Dynamic binding -- Qt's Signals and Slots vs Objective-C</a><br>
	<a href="http://www.digitalmars.com/d/archives/digitalmars/D/Dissecting_the_SS_42377.html">Dissecting the SS</a><br>
	<a href="http://www.digitalmars.com/d/archives/digitalmars/D/dwt/about_harmonia_454.html">about harmonia</a><br>
	<a href="http://www.digitalmars.com/d/archives/digitalmars/D/announce/1502.html">Another event handling module</a><br>
	<a href="http://www.digitalmars.com/d/archives/digitalmars/D/41825.html">Suggestion: signal/slot mechanism</a><br>
	<a href="http://www.digitalmars.com/d/archives/digitalmars/D/13251.html">Signals and slots?</a><br>
	<a href="http://www.digitalmars.com/d/archives/digitalmars/D/10714.html">Signals and slots ready for evaluation</a><br>
	<a href="http://www.digitalmars.com/d/archives/digitalmars/D/1393.html">Signals &amp; Slots for Walter</a><br>
	<a href="http://www.digitalmars.com/d/archives/28456.html">Signal/Slot mechanism?</a><br>
	<a href="http://www.digitalmars.com/d/archives/19470.html">Modern Features?</a><br>
	<a href="http://www.digitalmars.com/d/archives/16592.html">Delegates vs interfaces</a><br>
	<a href="http://www.digitalmars.com/d/archives/16583.html">The importance of component programming (properties, signals and slots, etc)</a><br>
	<a href="http://www.digitalmars.com/d/archives/16368.html">signals and slots</a><br><br><br>
<span style="color:red">BUGS:</span><br>
Slots can only be delegates formed from class objects or
	interfaces to class objects. If a delegate to something else
	is passed to connect&#40;&#41;, such as a struct member function,
	a nested function or a COM interface, undefined behavior
	will result.<br><br>	Not safe for multiple threads operating on the same signals
	or slots.<br><br>
<b>Author:</b><br>Walter Bright, Digital Mars, www.digitalmars.com<br><br>
<dl>
<dt><big>Object <a href="./htmlsrc/std.signals.html#L65" title="At line 65.">_d_toObject</a>(void* <i>p</i>); [extern(C)]</big></dt>
<dd></dd>
<dt><big>template <a href="./htmlsrc/std.signals.html#L132" title="At line 132.">Signal</a>(T1...);</big></dt>
<dd>
Mixin to create a signal within a class object.<br><br>
Different signals can be added to a class by naming the mixins.<br><br> <br><br>
<b>Example:</b><br><pre class="d_code">

<span class="k">import</span> <span class="i">std</span>.<span class="i">signals</span>;
<span class="k">import</span> <span class="i">std</span>.<span class="i">stdio</span>;

<span class="k">class</span> <span class="i">Observer</span>
{   <span class="lc">// our slot</span>
    <span class="k">void</span> <span class="i">watch</span>(<span class="k">char</span>[] <span class="i">msg</span>, <span class="k">int</span> <span class="i">i</span>)
    {
	<span class="i">writefln</span>(<span class="sl">"Observed msg '%s' and value %s"</span>, <span class="i">msg</span>, <span class="i">i</span>);
    }
}

<span class="k">class</span> <span class="i">Foo</span>
{
    <span class="k">int</span> <span class="i">value</span>() { <span class="k">return</span> <span class="i">_value</span>; }

    <span class="k">int</span> <span class="i">value</span>(<span class="k">int</span> <span class="i">v</span>)
    {
	<span class="k">if</span> (<span class="i">v</span> != <span class="i">_value</span>)
	{   <span class="i">_value</span> = <span class="i">v</span>;
	    <span class="lc">// call all the connected slots with the two parameters</span>
	    <span class="i">emit</span>(<span class="sl">"setting new value"</span>, <span class="i">v</span>);
	}
	<span class="k">return</span> <span class="i">v</span>;
    }

    <span class="lc">// Mix in all the code we need to make Foo into a signal</span>
    <span class="k">mixin</span> <span class="i">Signal</span>!(<span class="k">char</span>[], <span class="k">int</span>);

  <span class="k">private</span> :
    <span class="k">int</span> <span class="i">_value</span>;
}

<span class="k">void</span> <span class="i">main</span>()
{
    <span class="i">Foo</span> <span class="i">a</span> = <span class="k">new</span> <span class="i">Foo</span>;
    <span class="i">Observer</span> <span class="i">o</span> = <span class="k">new</span> <span class="i">Observer</span>;

    <span class="i">a</span>.<span class="i">value</span> = <span class="n">3</span>;		<span class="lc">// should not call o.watch()</span>
    <span class="i">a</span>.<span class="i">connect</span>(&amp;<span class="i">o</span>.<span class="i">watch</span>);	<span class="lc">// o.watch is the slot</span>
    <span class="i">a</span>.<span class="i">value</span> = <span class="n">4</span>;		<span class="lc">// should call o.watch()</span>
    <span class="i">a</span>.<span class="i">disconnect</span>(&amp;<span class="i">o</span>.<span class="i">watch</span>);	<span class="lc">// o.watch is no longer a slot</span>
    <span class="i">a</span>.<span class="i">value</span> = <span class="n">5</span>;		<span class="lc">// so should not call o.watch()</span>
    <span class="i">a</span>.<span class="i">connect</span>(&amp;<span class="i">o</span>.<span class="i">watch</span>);	<span class="lc">// connect again</span>
    <span class="i">a</span>.<span class="i">value</span> = <span class="n">6</span>;		<span class="lc">// should call o.watch()</span>
    <span class="k">delete</span> <span class="i">o</span>;			<span class="lc">// destroying o should automatically disconnect it</span>
    <span class="i">a</span>.<span class="i">value</span> = <span class="n">7</span>;		<span class="lc">// should not call o.watch()</span>
}

</pre>
 which should print:
 <pre>
 Observed msg 'setting new value' and value 4
 Observed msg 'setting new value' and value 6
 </pre><br><br>
<dl>
<dt><big>alias void delegate(T1) <a href="./htmlsrc/std.signals.html#L142" title="At line 142.">slot_t</a>;</big></dt>
<dd>
A slot is implemented as a delegate.
 The slot_t is the type of the delegate.
 The delegate must be to an instance of a class or an interface
 to a class instance.
 Delegates to struct instances or nested functions must not be
 used as slots.
     <br><br></dd>
<dt><big>void <a href="./htmlsrc/std.signals.html#L147" title="At line 147.">emit</a>(T1 <i>i</i>);</big></dt>
<dd>
Call each of the connected slots, passing the argument&#40;s&#41; i to them.
     <br><br></dd>
<dt><big>void <a href="./htmlsrc/std.signals.html#L158" title="At line 158.">connect</a>(slot_t <i>slot</i>);</big></dt>
<dd>
Add a slot to the list of slots to be called when emit&#40;&#41; is called.
     <br><br></dd>
<dt><big>void <a href="./htmlsrc/std.signals.html#L195" title="At line 195.">disconnect</a>(slot_t <i>slot</i>);</big></dt>
<dd>
Remove a slot from the list of slots to be called when emit&#40;&#41; is called.
     <br><br></dd>
<dt><big>void <a href="./htmlsrc/std.signals.html#L218" title="At line 218.">unhook</a>(Object <i>o</i>);</big></dt>
<dd></dd>
<dt><big>~<a href="./htmlsrc/std.signals.html#L236" title="At line 236.">this</a>();</big></dt>
<dd></dd>
<dt><big>slot_t[] <a href="./htmlsrc/std.signals.html#L258" title="At line 258.">slots</a>; [private]</big></dt>
<dd></dd>
<dt><big>size_t <a href="./htmlsrc/std.signals.html#L259" title="At line 259.">slots_idx</a>; [private]</big></dt>
<dd></dd></dl></dd>
<dt><big>void <a href="./htmlsrc/std.signals.html#L264" title="At line 264.">linkin</a>();</big></dt>
<dd></dd>
<dt><big><a href="./htmlsrc/std.signals.html#L266" title="At line 266.">unittest</a>;</big></dt>
<dd></dd></dl>
    <br><br>
<br><br>
<!-- Google ad -->
<script type="text/javascript"><!--
/**/google_ad_client = "pub-5628673096434613";
/**/google_ad_width = 728;
/**/google_ad_height = 90;
/**/google_ad_format = "728x90_as";
/**/google_ad_channel ="6203743411";
/**/google_page_url = document.location;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<div id="copyright">
Public Domain |
Page generated by <a href="http://code.google.com/p/dil">dil</a> on Wed Oct  1 23:47:52 2008
</div>

</body>
</html>